# Current, as of Thursday, May 28, 2020.
# rm(list = ls())
# setwd("/Users/ericschmidt/Dropbox/Base Dropbox/List Experiment Paper (PS)/Conditional Accept/Replication Files (Carmines-Schmidt PS)/Difference-of-Means Plots") 
library(foreign)
library(car)
library(readstata13)
library(zeligverse)
library(ggplot2)
#-----
# Load our spreadsheet of point estimates and related statistics. 
our.data = read.csv("Point-Estimates-2008-2012.csv")
names(our.data)
table(our.data$experiment)

# Reorder the levels of the "sample" variable to reflect the desired layout.
our.data$sample = factor(our.data$sample, levels = c("Full Sample",
                                                     "Democrats",
                                                     "Independents (or Unsure)",
                                                     "Republicans",
                                                     "Liberals",
                                                     "Moderates (or Unsure)",
                                                     "Conservatives"))

# Subset: 2008 data; models reported in the paper (Figure 2). 
data.2008.paper = subset(our.data, year==2008 & model_version=="Reported" & 
                           experiment!="Sensitive-Items Pooled")
# Subset: 2008 data; models reported in the appendix (Figure S2). 
data.2008.appendix = subset(our.data, year==2008 & model_version=="Appendix" & 
                           experiment!="Sensitive-Items Pooled")
# Subset: 2012 data; models reported in the paper (Figure 3). 
data.2012.paper = subset(our.data, year==2012 & model_version=="Reported" & 
                           experiment!="Sensitive-Items Pooled")
# Subset: 2012 data; models reported in the appendix (Figure S3). 
data.2012.appendix = subset(our.data, year==2012 & model_version=="Appendix" & 
                              experiment!="Sensitive-Items Pooled")
# Subset: 2008 and 2012 data; generalized prejudice; models reported in the paper (Figure 4). 
pooled.paper = subset(our.data, model_version=="Reported" & 
                           experiment=="Sensitive-Items Pooled")
# Subset: 2008 and 2012 data; generalized prejudice; models reported in the appendix (Figure S4). 
pooled.appendix = subset(our.data, model_version=="Appendix" & 
                              experiment=="Sensitive-Items Pooled")
#-----
# Figure 2: 2008 differences-of-means (individual experiments versus baseline, 
# by PID and ideology)
pd = position_dodge(.9)
# wrapper = function(x, ...) 
# {
#  paste(strwrap(x, ...), collapse = "\n")
# }
# our.caption = "Data from the 2008 Cooperative Campaign Analysis Project (n = 1338). The 'African-American candidate' list experiment was analyzed using only respondents that did not identify as 'Black.' All other list experiments were analyzed using the full sample. Vertical lines indicate 90 percent confidence intervals produced using normal linear regression in zelig (Choirat et al. 2018). Treatment effects imply the proportion of the sample (or subsample) that was more likely to vote AGAINST a candidate that matched the description. Horizontal dashed lines indicate effect sizes that each comparison is equipped to detect (given the number of respondents in treatment and control) with 80 percent power; power analyses conducted using DeclareDesign (Blair et al. 2019)."
our.plot = ggplot(data.2008.paper, aes(fill = sample, y = PE, x = sample)) +
  geom_bar(position=pd, stat="identity") +
  geom_errorbar(aes(ymin = LB, ymax = UB), size = .3, width = .1, position = pd) +
  geom_errorbar(aes(ymin = ate_power80, ymax = ate_power80), 
                linetype = "dashed", size = .3, width = 0.9, position = pd) + 
  facet_wrap( ~ experiment, nrow = 2) + theme_minimal() + 
  scale_fill_grey(name = "Subset of Respondents") + ylab("Treatment Effect") +
#  ggtitle("Figure 2: List Experiments, 2008 CCAP: Estimated Proportions \nMore Likely to Vote AGAINST the Candidate-in-Question",
#          subtitle = "Horizontal dashed lines indicate effect sizes that can be detected at 80 percent power (d = 0.80).") + 
#  theme(plot.title = element_text(hjust = 0.5, face = "bold")) + 
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank()) + 
  theme(strip.text = element_text(size = 10, face = "bold")) # + 
#  theme(plot.subtitle = element_text(hjust = 0.5, face = "italic")) + 
#  theme(plot.caption = element_text(hjust = 0, size = 9)) +
#  labs(caption = wrapper(our.caption, width = 120)) 
our.plot
ggsave(plot = our.plot, file = "Fig2.eps", height = 6, width = 9, units = "in")
# ggsave(plot = our.plot, file = "Figure-2-2008.pdf", height = 6, width = 9, units = "in")
#-----
# Figure S2: 2008 differences-of-means (individual experiments versus baseline, 
# by PID and ideology); for appendix (models estimated using confounding variables)
pd = position_dodge(.9)
wrapper = function(x, ...) 
{
  paste(strwrap(x, ...), collapse = "\n")
}
our.caption = "Data from the 2008 Cooperative Campaign Analysis Project. The 'African-American candidate' list experiment was analyzed using only respondents that did not identify as 'Black.' All other list experiments were analyzed using the full sample. Vertical lines indicate 90 percent confidence intervals produced using normal linear regression in zelig (Choirat et al. 2018). Treatment effects imply the proportion of the sample (or subsample) that was more likely to vote AGAINST a candidate that matched the description."
our.plot = ggplot(data.2008.appendix, aes(fill = sample, y = PE, x = sample)) +
  geom_bar(position=pd, stat="identity") +
  geom_errorbar(aes(ymin = LB, ymax = UB), size = .3, width = .1, position = pd) +
  # geom_errorbar(aes(ymin = ate_power80, ymax = ate_power80), 
  #              linetype = "dashed", size = .3, width = 0.9, position = pd) + 
  facet_wrap( ~ experiment, nrow = 2) + theme_minimal() + 
  scale_fill_grey(name = "Subset of Respondents") + ylab("Treatment Effect") +
  ggtitle("List Experiments, 2008 CCAP: Estimated Proportions \nMore Likely to Vote AGAINST the Candidate-in-Question",
          subtitle = "Models estimated using confounding variables; see appendix tables for details.") + 
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) + 
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank()) + 
  theme(strip.text = element_text(size = 10, face = "bold")) + 
  theme(plot.subtitle = element_text(hjust = 0.5, face = "italic")) + 
  theme(plot.caption = element_text(hjust = 0, size = 9)) +
  labs(caption = wrapper(our.caption, width = 120)) 
our.plot
ggsave(plot = our.plot, file = "Figure-S2-2008-Appendix.pdf", height = 6, width = 9, units = "in")
#-----
# Figure 3: 2012 differences-of-means (individual experiments versus baseline, 
# by PID and ideology)
pd = position_dodge(.9)
# wrapper = function(x, ...) 
# {
#  paste(strwrap(x, ...), collapse = "\n")
# }
# our.caption = "Data from the 2012 Cooperative Campaign Analysis Project (n = 1000). The 'African-American candidate' list experiment was analyzed using only respondents that did not identify as 'Black.' All other list experiments were analyzed using the full sample. Vertical lines indicate 90 percent confidence intervals produced using normal linear regression in zelig (Choirat et al. 2018). Treatment effects imply the proportion of the sample (or subsample) that was more likely to vote AGAINST a candidate that matched the description. Horizontal dashed lines indicate effect sizes that each comparison is equipped to detect (given the number of respondents in treatment and control) with 80 percent power; power analyses conducted using DeclareDesign (Blair et al. 2019)."
our.plot = ggplot(data.2012.paper, aes(fill = sample, y = PE, x = sample)) +
  geom_bar(position=pd, stat="identity") +
  geom_errorbar(aes(ymin = LB, ymax = UB), size = .3, width = .1, position = pd) +
  geom_errorbar(aes(ymin = ate_power80, ymax = ate_power80), 
                linetype = "dashed", size = .3, width = 0.9, position = pd) + 
  facet_wrap( ~ experiment, nrow = 2) + theme_minimal() + 
  scale_fill_grey(name = "Subset of Respondents") + ylab("Treatment Effect") +
#  ggtitle("Figure 3: List Experiments, 2012 CCAP: Estimated Proportions \nMore Likely to Vote AGAINST the Candidate-in-Question",
#          subtitle = "Horizontal dashed lines indicate effect sizes that can be detected at 80 percent power (d = 0.80).") + 
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) + 
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank()) + 
  theme(strip.text = element_text(size = 10, face = "bold")) # + 
#  theme(plot.subtitle = element_text(hjust = 0.5, face = "italic")) + 
#  theme(plot.caption = element_text(hjust = 0, size = 9)) +
#  labs(caption = wrapper(our.caption, width = 120)) 
our.plot
ggsave(plot = our.plot, file = "Fig3.eps", height = 6, width = 9, units = "in")
# ggsave(plot = our.plot, file = "Figure-3-2012.pdf", height = 6, width = 9, units = "in")

# Figure S3: 2012 differences-of-means (individual experiments versus baseline, 
# by PID and ideology); for appendix (models estimated using confounding variables)
pd = position_dodge(.9)
wrapper = function(x, ...) 
{
  paste(strwrap(x, ...), collapse = "\n")
}
our.caption = "Data from the 2012 Cooperative Campaign Analysis Project. The 'African-American candidate' list experiment was analyzed using only respondents that did not identify as 'Black.' All other list experiments were analyzed using the full sample. Vertical lines indicate 90 percent confidence intervals produced using normal linear regression in zelig (Choirat et al. 2018). Treatment effects imply the proportion of the sample (or subsample) that was more likely to vote against a candidate that matched the description."
our.plot = ggplot(data.2012.appendix, aes(fill = sample, y = PE, x = sample)) +
  geom_bar(position=pd, stat="identity") +
  geom_errorbar(aes(ymin = LB, ymax = UB), size = .3, width = .1, position = pd) +
  # geom_errorbar(aes(ymin = ate_power80, ymax = ate_power80), 
  #              linetype = "dashed", size = .3, width = 0.9, position = pd) + 
  facet_wrap( ~ experiment, nrow = 2) + theme_minimal() + 
  scale_fill_grey(name = "Subset of Respondents") + ylab("Treatment Effect") +
  ggtitle("List Experiments, 2012 CCAP: Estimated Proportions \nMore Likely to Vote AGAINST the Candidate-in-Question",
          subtitle = "Models estimated using confounding variables; see appendix tables for details.") + 
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) + 
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank()) + 
  theme(strip.text = element_text(size = 10, face = "bold")) + 
  theme(plot.subtitle = element_text(hjust = 0.5, face = "italic")) + 
  theme(plot.caption = element_text(hjust = 0, size = 9)) +
  labs(caption = wrapper(our.caption, width = 120)) 
our.plot
ggsave(plot = our.plot, file = "Figure-S3-2012-Appendix.pdf", height = 6, width = 9, units = "in")
#-----
# Figure 4: 2008 and 2012, generalized prejudice (pooled sensitive-items versus baseline, 
# by PID and ideology)

# Year variable; rename the levels. 
pooled.paper$year = factor(pooled.paper$year)
levels(pooled.paper$year) = c("2008 CCAP (n = 1338)", "2012 CCAP (n = 1000)")
table(pooled.paper$year)

# Plot.
pd = position_dodge(.9)
# wrapper = function(x, ...) 
# {
#  paste(strwrap(x, ...), collapse = "\n")
# }
# our.caption = "Data from the 2008 (n = 1338) and 2012 (n = 1000) Cooperative Campaign Analysis Projects. Treatment effects are calculated by comparing respondents in the control condition (2008, n = 260; 2012, n = 185) to respondents in all four sensitive-item conditions (2008, n = 1078; 2012, n = 815); treatment effects imply the proportion of the sample (or subsample) that was more likely to vote AGAINST a presidential candidate from an underrepresented group. Vertical lines indicate 90 percent confidence intervals produced using normal linear regression in zelig (Choirat et al. 2018). Horizontal dashed lines indicate effect sizes that each comparison is equipped to detect (given the number of respondents in treatment and control) with 80 percent power; power analyses conducted using DeclareDesign (Blair et al. 2019)."
our.plot = ggplot(pooled.paper, aes(fill = sample, y = PE, x = sample)) +
  geom_bar(position=pd, stat="identity") +
  geom_errorbar(aes(ymin = LB, ymax = UB), size = .3, width = .1, position = pd) +
  geom_errorbar(aes(ymin = ate_power80, ymax = ate_power80), 
                linetype = "dashed", size = .3, width = 0.9, position = pd) + 
  facet_wrap( ~ year, nrow = 2) + theme_minimal() + 
  scale_fill_grey(name = "Subset of Respondents") + ylab("Treatment Effect") +
  # ggtitle("Figure 4: Generalized Prejudice, 2008 and 2012 CCAP: Estimated Proportions \nMore Likely to Vote AGAINST Candidates from Underrepresented Groups",
  #        subtitle = "Horizontal dashed lines indicate effect sizes that can be detected at 80 percent power (d = 0.80).") + 
#  theme(plot.title = element_text(hjust = 0.5, face = "bold")) + 
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank()) + 
  theme(strip.text = element_text(size = 10, face = "bold")) # + 
#  theme(plot.subtitle = element_text(hjust = 0.5, face = "italic")) + 
#  theme(plot.caption = element_text(hjust = 0, size = 9)) +
#  labs(caption = wrapper(our.caption, width = 120)) 
our.plot
ggsave(plot = our.plot, file = "Fig4.eps", height = 6, width = 9, units = "in")
# ggsave(plot = our.plot, file = "Figure-4-General-Prejudice.pdf", height = 6, width = 9, units = "in")
#-----
# Figure S4: 2008 and 2012, generalized prejudice (pooled sensitive-items versus baseline, 
# by PID and ideology), including models with confounding variables.

# Year variable; rename the levels. 
pooled.appendix$year = factor(pooled.appendix$year)
levels(pooled.appendix$year) = c("2008 CCAP", "2012 CCAP")
table(pooled.appendix$year)

# Plot.
pd = position_dodge(.9)
wrapper = function(x, ...) 
{
  paste(strwrap(x, ...), collapse = "\n")
}
our.caption = "Data from the 2008 and 2012 Cooperative Campaign Analysis Project. Treatment effects are calculated by comparing respondents in the control condition to respondents in all four sensitive-item conditions; treatment effects imply the proportion of the sample (or subsample) that was more likely to vote AGAINST a presidential candidate from an underrepresented group. Vertical lines indicate 90 percent confidence intervals produced using normal linear regression in zelig (Choirat et al. 2018)." 
our.plot = ggplot(pooled.appendix, aes(fill = sample, y = PE, x = sample)) +
  geom_bar(position=pd, stat="identity") +
  geom_errorbar(aes(ymin = LB, ymax = UB), size = .3, width = .1, position = pd) +
  facet_wrap( ~ year, nrow = 2) + theme_minimal() + 
  scale_fill_grey(name = "Subset of Respondents") + ylab("Treatment Effect") +
  ggtitle("Generalized Prejudice, 2008 and 2012 CCAP: Estimated Proportions \nMore Likely to Vote AGAINST Candidates from Underrepresented Groups",
          subtitle = "Models estimated using confounding variables; see appendix tables for details.") + 
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) + 
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank()) + 
  theme(strip.text = element_text(size = 10, face = "bold")) + 
  theme(plot.subtitle = element_text(hjust = 0.5, face = "italic")) + 
  theme(plot.caption = element_text(hjust = 0, size = 9)) +
  labs(caption = wrapper(our.caption, width = 120)) 
our.plot
ggsave(plot = our.plot, file = "Figure-S4-General-Prejudice-Appendix.pdf", height = 6, width = 9, units = "in")
#-----

